<template>
  <Drawer :title="title">
    <!-- 表单 -->
    <FosoForm
      ref="formRef"
      :form-columns="formColumns"
      :form-data="formData"
      :form-rules="formRules"
      label-position="top"
      label-width="100px"
    >
      <template #avater>
        <!-- 头像 -->
        <el-upload
          :before-upload="beforeAvatarUpload"
          :on-success="handleAvatarSuccess"
          :show-file-list="false"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          class="avatar-uploader"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </template>
      <!-- 图标组件 -->
      <template #gender> <Test /> </template>
    </FosoForm>
  </Drawer>
</template>

<script lang="ts" setup>
import type { UploadProps } from 'element-plus';

import { useVbenDrawer } from '@vben/common-ui';
import { $t } from '@vben/locales';

import { Plus } from '@element-plus/icons-vue';

import Test from '../../oauth/test.vue';

defineProps({
  // 表单数据
  title: {
    type: String,
    default: '',
  },
});

const imageUrl = ref(''); // 头像图片的 URL

// 头像上传成功的钩子，参数为上传的文件和上传的响应数据
const handleAvatarSuccess: UploadProps['onSuccess'] = (uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!);
};

// 头像上传前的钩子，参数为上传的文件
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!');
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!');
    return false;
  }
  return true;
};

// 校验规则
const formRules = ref({
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
});

// 弹窗连接
const [Drawer, drawerApi] = useVbenDrawer({
  onConfirm() {
    // console.info('onConfirm');
  },
  onCancel() {
    drawerApi.close();
  },
});

// 表单实例
const formRef = ref();

// 表单数据
const formData = ref({
  name: '',
  age: 18,
  gender: '',
  munetype: 'dir',
  gender2: '',
  checkedCities: ['Shanghai', 'Beijing', 'Guangzhou', 'Shenzhen'],
  gender3: '',
  cascader: [],
  allselect: false,
  gender4: '',
  tree: [], // Tree 组件绑定的数据
});

// 表单配置项
const formColumns = computed(() => [
  {
    label: $t('sys.user.avatar'), // 表单项的标签,
    prop: 'avater',
    xType: 'upload',
    span: 24,
  },
  {
    label: $t('sys.login.username'),
    prop: 'name',
    xType: 'Input',
    span: 24,
    hidden: false,
    componentProps: {
      placeholder: $t('common.form.pleaseInput'),
      clearable: true,
    },
  },
  {
    label: $t('sys.user.nickname'),
    prop: 'name',
    xType: 'Input',
    span: 24,
    componentProps: {
      placeholder: $t('common.form.pleaseInput'),
      clearable: true,
    },
  },
  {
    label: $t('sys.user.description'),
    prop: 'gender2', // 绑定到 formData.gender
    xType: 'Select',
    span: 24,
    options: [
      { label: '男', value: 'male' },
      { label: '女', value: 'female' },
    ],
    componentProps: {
      clearable: true, // el-select 的属性
    },
  },
  {
    label: $t('sys.user.homePath'),
    prop: 'gender', // 绑定到 formData.gender
    xType: 'Select',
    span: 24,
    options: [
      { label: '男', value: 'male' },
      { label: '女', value: 'female' },
    ],
    componentProps: {
      clearable: true, // el-select 的属性
    },
  },
  {
    label: $t('sys.login.mobile'),
    prop: 'name',
    xType: 'Input',
    span: 24, // 自行配置行宽
    componentProps: {
      placeholder: $t('common.form.pleaseInput'),
      clearable: true,
    },
  },
  {
    label: $t('sys.login.email'),
    prop: 'name',
    xType: 'Input',
    span: 24, // 自行配置行宽
    componentProps: {
      placeholder: $t('common.form.pleaseInput'),
      clearable: true,
    },
  },
  {
    label: $t('sys.login.password'),
    prop: 'name',
    xType: 'Input',
    span: 24, // 自行配置行宽
    componentProps: {
      placeholder: $t('common.form.pleaseInput'),
      clearable: true,
    },
  },
  {
    label: $t('sys.role.roleTitle'),
    prop: 'name',
    xType: 'Input',
    span: 24, // 自行配置行宽
    componentProps: {
      placeholder: $t('common.form.pleaseInput'),
      clearable: true,
    },
  },
  {
    label: $t('sys.department.userDepartment'),
    prop: 'name66',
    xType: 'Input',
    span: 24, // 自行配置行宽
    componentProps: {
      placeholder: $t('common.form.pleaseInput'),
      clearable: true,
    },
  },
  {
    label: $t('sys.position.userPosition'),
    prop: 'name',
    xType: 'Input',
    span: 24, // 自行配置行宽
    componentProps: {
      placeholder: $t('common.form.pleaseInput'),
      clearable: true,
    },
  },
  {
    label: $t('sys.menu.permission'),
    prop: 'name',
    xType: 'Input',
    span: 24, // 自行配置行宽
    componentProps: {
      placeholder: $t('common.form.pleaseInput'),
      clearable: true,
    },
  },
  {
    label: $t('sys.role.status'),
    prop: 'munetype',
    xType: 'RadioGroup',
    componentProps: {
      options: [
        { label: $t('sys.role.start'), value: 'dir' },
        { label: $t('sys.role.stop'), value: 'menu' },
      ],
    },
  },
]);
</script>
<style scoped>
.avatar-uploader .avatar {
  display: block;
  width: 178px;
  height: 178px;
}
</style>

<style>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  border-radius: 50%;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  width: 178px;
  height: 178px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
  border-radius: 50%;
}
</style>
